<?php

class luck extends bhCommon {

    public $vote;

    public function __construct()
    {
        parent::__construct();
        $this->vote = new vote();
    }




    public function index()
    {
        $luck = p_getall('luck', array('is_delete' => 1, 'status' => 1));
        if ($luck) {
            foreach ($luck as & $v) {
                $v['goods'] = p_getall('luck_goods', array('luck_id' => $v['id']));

                $v['image'] = getImage($v['goods'][0]['image']);

                if ($v['lottery_type'] == 1) {
                    $v['lottery'] = date('m月d日 H:i', $v['lottery_value']);
                } elseif ($v['lottery_type'] == 2) {
                    $v['lottery'] = '满' . $v['lottery_value'] . '人';
                } else {
                    $v['lottery'] = '发起者手动开奖';
                }

                $v['is_apply'] = p_getcolumn('luck_participate', array('luck_id' => $v['id'], 'member_id' => $this->member['id']), 'COUNT(*)') > 0;
            }
        }
        $data = array(
            'luck' => $luck,
            'banner_id' => getConfig('luck_ad'),
            'luck_title' => getConfig('luck_title', '免费抽奖')
        );
        $this->json($data);
    }


    /**
     *
     */
    public function init()
    {
        $prize_default_picture = $this->images('prize_default_picture', 'prize_default_picture.png');
        $prize_default_picture_path = $this->images('prize_default_picture', 'prize_default_picture.png', true);
        $time = time();
        $toady = strtotime(date('Y-m-d', $time));

        $lottery_time = array();
        $is = array(
            0 => '(今天)',
            1 => '(1天后)',
            2 => '(2天后)',
            3 => '(3天后)',
            4 => '(4天后)',
            5 => '(5天后)',
            6 => '(6天后)',
            7 => '(7天后)',
            8 => '(8天后)',
            9 => '(9天后)',
            10 => '(10天后)',
            11 => '(11天后)',
            12 => '(12天后)',
            13 => '(13天后)',
            14 => '(14天后)',
            15 => '(15天后)',
        );
        for ($i = 0; $i < 16; $i++) {
            $t = $toady + $i * 86400;
            $lottery_time['yd'][$t] = array(
                't' => date('m月d日', $t),
                'w' => week($t),
                'i' => $is[$i]
            );
            $lottery_time['yd_s'][] = $t;
        }

        $lottery_time['h'] = date('H', $time);
        $lottery_time['i'] = date('i', $time);
        $lottery_time['d'] = $toady + 86400;

        $images = array(
            'icn_s' => $this->image_url . 'icn-s.png',
            'icon_y' => $this->image_url . 'icon-y.png',
            'icon_h' => $this->image_url . 'icn-h.png',
            'play3x' => $this->image_url . 'play3x.png',
        );
        $team_number = getConfig('team_number');

        $data = array(
            'is_luck' => $this->member['is_luck'],
            'prize_default_picture' => array(
                'url' => $prize_default_picture,
                'path' => $prize_default_picture_path
            ),
            'senior_number' => p_getcolumn('senior_member', array('member_id' => $this->member['id']), 'number') ? : 0,
            'prize' => array(
                array(
                    'url' => $prize_default_picture,
                    'image' => '',
                    'type' => 1
                )
            ),
            'max_number' => 999,
            'time' => $time,
            'lottery_time' => $lottery_time,
            'lottery_max_number' => getConfig('lottery_max_number', 1024),
            'tips' => array(
                'pwd' => getConfig('luck_tips_pwd', '需要输入口令才能抽奖,可使用关注公众号或加好友的方式获取口令'),
                'group' => getConfig('luck_tips_group', '组队后,一人中奖,全队中奖,团队需要达到成团人数后才能参与'),
                'addition' => getConfig('luck_tips_addition', '设置后,用户可快速获取你的公众号/微信号二维码,高效加粉'),
                'appoint_group' => getConfig('appoint_group_msg', '设置后,分享到的第一个群的群成员才能参与抽奖'),
                'code_tips' => getConfig('code_tips', '参'),
                'share_tips' => getConfig('share_tips', '开启后,参与者可分享抽奖给TA的好友,帮助抽奖快速传播'),
            ),
            'images' => $images,
            'team_numbers' => $team_number ? json_decode($team_number, true) : array(),
            'use_substance' => getConfig('use_substance', 1),
            'launch_ad' => getConfig('launch_ad', 1),
            'dy_send_message_kj_id' => getConfig('dy_send_message_kj_id'),
            'open_senior' => getConfig('open_senior', 1),
            'user_look_video' => getConfig('user_look_video', 1),
            'luck_examine' => getConfig('luck_examine', 2) ? : 2
        );

        $this->json($data);
    }


    public function te()
    {
        $id = $this->get('id');
        //$res = $this->vote->lottery($id);
        //var_dump($res);
        var_dump($this->vote->error);
    }


    /**
     *
     */
    public function participate()
    {
        $id = $this->get('id');
        if (p_get('luck_participate', array('luck_id' => $id, 'member_id' => $this->member['id']))) {
            $this->json();
        }

        $luck = p_get('luck', array('id' => $id, 'is_delete' => 1, 'status' => 1, 'complete' => 2));
        if (empty($luck)) {
            $this->json('该抽奖不存在或已结束', 2);
        }
        $participate_group_id = 0;

        if ($luck['appoint_group'] == 2) {
            $encryptedData = $this->get('encryptedData');
            $iv = $this->get('iv');

            if ($encryptedData && $iv) {
                $wxbd = new WXBizDataCrypt($this->w['uniaccount']['key'], $this->session_key);
                $res = $wxbd->decryptData($encryptedData, $iv, $data);
                if ($res != ErrorCode::$OK) {
                    $this->json('unlogin', -1);
                }
                $data = json_decode($data, true);

                $share_group = p_get('luck_participate_group', array('luck_id' => $id, 'group_id' => $data['openGId']));

                if (empty($share_group)) {
                    $insert = array(
                        'luck_id' => $id,
                        'number' => 1,
                        'group_id' => $data['openGId'],
                        'created' => time()
                    );

                    if (!p_insert('luck_participate_group', $insert)) {
                        $this->json('抽奖失败1', 2);
                    }
                    $participate_group_id = pdo_insertid();
                } else {
                    $participate_group_id = $share_group['id'];

                    if (!p_update('luck_participate_group', array('number' => $share_group['number'] + 1), array('id' => $share_group['id']))) {
                        $this->json('抽奖失败2', 2);
                    }
                }

                if ($data['openGId'] != $luck['group_id']) {
                    //$this->json('该抽奖目前只有指定群进入才能参与', 2);
                }
            } else {
                $this->json('该抽奖目前只有指定群进入才能参与', 2);
            }
        }

        if ($luck['luck_mode'] == 4) {
            $luck_pwd = $this->get('luck_pwd');
            if ($luck_pwd != $luck['luck_pwd']) {
                $this->json('请填写正确的口令', 2);
            }
        }

        pdo_begin();
        try{
            $insert = array(
                'luck_id' => $id,
                'member_id' => $this->member['id'],
                'created' => time(),
                'participate_group_id' => $participate_group_id
            );

            if ($luck['is_ad'] == 3 && $this->vote->isIntegralUser()) {
                if (!$this->vote->change_integral($this->member['id'], $luck['integral'], 7, '积分抽奖')) {
                    $this->json($this->vote->error, 2);
                }
            }

            if ($luck['luck_mode'] == 2) {
                $number = 0;
                $group_id = $this->get('group_id');
                if ($group_id) {
                    $group = p_get('luck_group', array('id' => $group_id, 'luck_id' => $id));
                    if ($group) {
                        $count = p_getcolumn('luck_participate', array('group_id' => $group_id), 'COUNT(*)') ? : 0;
                        $number = $group['max_number'] - $count;
                    }
                }

                if ($number > 0) {
                    $insert['group_id'] = $group_id;
                    p_update('luck_group', array('current_number' => $group['current_number'] + 1), array('id' => $group['id']));
                } else {
                    $i_group = array(
                        'member_id' => $this->member['id'],
                        'luck_id' => $id,
                        'max_number' => $luck['team_number'],
                        'current_number' => 1,
                        'created' => time()
                    );
                    if (!p_insert('luck_group', $i_group)) {
                        throw new Exception('参与失败1');
                    }
                    $insert['group_id'] = pdo_insertid();
                }
            } else if ($luck['luck_mode'] == 3) {
                $share_id = $this->get('share_id');
                if (!$this->vote->create_luck_code($this->member['id'], $id, $share_id)) {
                    throw new Exception('参与失败2');
                }

            }
            if (!p_insert('luck_participate', $insert)) {
                throw new Exception('参与失败3');
            }

            pdo_commit();

            if ($luck['lottery_type'] == 2) {
                $count = p_getcolumn('luck_participate', array('luck_id' => $id), 'COUNT(*)') ? : 0;
                if ($count >= $luck['lottery_value']) {
                    $this->vote->lottery($id);
                }
            }

            $this->json('');
        } catch (Exception $e) {
            pdo_rollback();
            $this->json($e->getMessage(), 2);
        }
    }

    public function groupList()
    {
        $id = intval($this->get('id'));

        if (!$luck = $this->vote->luck_info($id, $this)) {
            $this->json('该抽奖不存在', 0);
        }
        if ($luck['member_id'] != $this->member['id']) {
            $this->json('无权限查看', 0);
        }

        $luck_goods = p_get('luck_goods', array('luck_id' => $id), array(), '', 'id desc');

        if ($luck_goods['image']) {
            $image = getImageLocal($this, $luck_goods['image']);
        } else {
            $image = getConfig('prize_default_picture');
            if ($image) {
                $image = getImageLocal($this, $image);
            } else {
                $image = $this->image_url . 'prize_default_picture.png';
            }
        }

        if ($luck['lottery_type'] == 1) {
            $font_text = date('m月d日 H:i', $luck['lottery_value']) . ' 自动开奖';
        } elseif ($luck['lottery_type'] == 2) {
            $font_text = '满' . $luck['lottery_value'] . '人 自动开奖';
        } else {
            $font_text = '发起者手动开奖';
        }
        $font = '奖品: ' . $luck['goods'][0]['name'];

        $new_share = array(
            'images' => $image,
            'font_text' => $font_text,
            'font' => $font
        );

        $group = p_getall('luck_participate_group', array('luck_id' => $id));
        $data = array(
            'luck' => $luck,
            'group' => $group,
            'new_share' => $new_share,
            'member' => $this->member
        );
        $this->json($data);
    }


    /**
     *
     */
    public function details()
    {
        $id = intval($this->get('id'));

        if (!$luck = $this->vote->luck_info($id, $this)) {
            $this->json('该抽奖不存在', 0);
        }

        if (false && $luck['appoint_group'] == 2 && empty($luck['group_id'])) {
            $encryptedData = $this->get('encryptedData');
            $iv = $this->get('iv');

            //$this->json(array('k' => 1), 1);

            if ($encryptedData && $iv) {
                $wxbd = new WXBizDataCrypt($this->w['uniaccount']['key'], $this->session_key);
                $res = $wxbd->decryptData($encryptedData, $iv, $data);
                if ($res != ErrorCode::$OK) {
                    $this->json('unlogin', -1);
                }
                $data = json_decode($data, true);

                p_update('luck', array('group_id' => $data['openGId']), array('id' => $id));
            }
        }

        $luck_goods = p_get('luck_goods', array('luck_id' => $id), array(), '', 'id desc');

        if ($luck_goods['image']) {
            $image = getImageLocal($this, $luck_goods['image']);
        } else {
            $image = getConfig('prize_default_picture');
            if ($image) {
                $image = getImageLocal($this, $image);
            } else {
                $image = $this->image_url . 'prize_default_picture.png';
            }
        }

        if ($luck['lottery_type'] == 1) {
            $font_text = date('m月d日 H:i', $luck['lottery_value']) . ' 自动开奖';
        } elseif ($luck['lottery_type'] == 2) {
            $font_text = '满' . $luck['lottery_value'] . '人 自动开奖';
        } else {
            $font_text = '发起者手动开奖';
        }
        $font = '奖品: ' . $luck['goods'][0]['name'];

        $new_share = array(
            'images' => $image,
            'font_text' => $font_text,
            'font' => $font
        );


        p_update('luck', array('exposure_number' => $luck['exposure_number'] + 1));

        $is_own = $this->member['id'] == $luck['member_id'];
        $luck['lottery_type'] == 1 && $luck['lottery_value'] = date('m月d日 H:i', $luck['lottery_value']);

        $launch = $luck['member_id'] ? p_get('member', array('id' => $luck['member_id'])) : array();

        $images = array(
            'icn_xcx' => $this->image_url . 'icn-xcx.png',
            'quote' => $this->image_url . 'quote.png',
            'icn_group' => $this->image_url . 'icn-group.png',
            'button_line' => $this->image_url . 'button-line.png',
            'icn_share' => $this->image_url . 'icn-share.png',
            'wx' => $this->image_url . 'wxi.png',
            'pyq' => $this->image_url . 'pyq.png',
            'zhh' => $this->image_url . 'zhh.png',
            'close' => $this->image_url . 'close.png',
            'tuan' => $this->image_url . 'tuan.png',
            'share_add' => $this->image_url . 'share_add.png',
            'authentication' => $this->image_url . 'authentication.png',
            'question3x' => $this->image_url . 'question3x.png',
        );


        if ($luck['sponsor'] && $luck['sponsor_type'] == 2 || $luck['sponsor_type'] == 5) {
            $luck['sponsor_text'] = json_decode($luck['sponsor_text'], true);
            if ($luck['sponsor_text']['type'] == 1) {
                $luck['sponsor_text']['image'] = getImage($luck['sponsor_text']['image']);
            }
        }

        $my_participate = p_get('luck_participate', array('luck_id' => $id, 'member_id' => $this->member['id']));
        $is_participate = !empty($my_participate);

        $group_id = $this->get('group_id', 0);
        $group = array();
        $assistance = array();
        if ($luck['luck_mode'] == 2) {
            if ($is_participate) {
                $group_id = $my_participate['group_id'];
            }
            if ($group_id) {
                $group = p_get('luck_group', array('id' => $group_id, 'luck_id' => $id));
                if ($group) {
                    $group['join'] = p_fetchall('SELECT B.headimgurl,B.id FROM ' . prefix('luck_participate') . ' AS A LEFT JOIN ' . prefix('member') . ' AS B ON A.member_id = B.id WHERE A.luck_id=' . $id .' AND A.group_id = '.$group_id.' ORDER BY A.id ASC');
                }
            }
        } elseif ($luck['luck_mode'] == 3 && $is_participate) {
            $assistance = array(
                'number' => p_getcolumn('luck_code', array('luck_id' => $id, 'member_id' => $this->member['id'], 'type' => 2), 'COUNT(*)') ? : 0,
                'user' => array(),
                'info_code_tips' => getConfig('info_code_tips')
            );
            $assistance['info_code_tips'] = $assistance['info_code_tips'] ? json_decode($assistance['info_code_tips'], true) : array();
            if ($assistance['number'] > 0) {
                $sql = "SELECT B.nickname,B.headimgurl FROM " . prefix('luck_code') . ' AS A INNER JOIN ' . prefix('member') . " AS B ON A.invitation_member_id=B.id WHERE A.member_id={$this->member['id']} AND A.luck_id={$id} AND A.type=2";
                $user = p_fetchall($sql);

                $assistance['user'] = $user;
            }
        }

        $participate = array(
            'num' => p_getcolumn('luck_participate', array('luck_id' => $id), 'COUNT(*)') ? :0,
            'list' => p_fetchall('SELECT B.headimgurl FROM ' . prefix('luck_participate') . ' AS A LEFT JOIN ' . prefix('member') . ' AS B ON A.member_id = B.id WHERE A.luck_id=' . $id .' AND A.member_id != '.$this->member['id'].' ORDER BY A.id DESC LIMIT 6')
        );

        if ($luck['lottery_type'] != 2 && $luck['automatic'] == 2) {
            $num = $this->vote->automatic_number($luck['created']);
            if ($participate['num'] < 6 && $num > 0) {
                $l = 6 - $participate['num'];
                $l = $num > $l ? $l : $num;
                $sql = 'SELECT headimgurl FROM ' . prefix('member') . ' WHERE fictitious = 2 LIMIT ' . $l;
                $automatic = p_fetchall($sql);

                $participate['list'] = array_merge($participate['list'], $automatic);
            }

            $participate['num'] += $num;
        }

        $after_lottery = array();
        if ($luck['status'] != 1) {
            $after_lottery = array(
                'ad' => '' //$this->vote->getAdvertisement(5, 2)
            );

            $after_lottery['in_my'] = false;
            if ($luck['status'] == 2) {
                $after_lottery['address'] = 0;
                //组队
                if ($luck['luck_mode'] == 2) {
                    $luck_group = p_getall('luck_group', array('luck_id' => $id, 'in_prize' => 2));
                    foreach ($luck_group as $k => $d) {
                        $luck_group[$k]['participate'] = p_getall('luck_participate', array('group_id' => $d['id']));
                        $luck_group[$k]['goods'] = p_get('luck_goods', array('id' => $d['luck_goods_id']));
                        foreach ($luck_group[$k]['participate'] as & $value) {
                            $value['member'] = p_get('member', array('id' => $value['member_id']));
                            if ($value['member_id'] == $this->member['id']) {
                                $after_lottery['in_my'] = true;
                                $after_lottery['in_my_goods'] = p_get('luck_goods_info', array('id' => $value['goods_info_id']));
                                $address = p_get('address', array('member_id' => $this->member['id']));
                                $after_lottery['in_my_address'] = !empty($address);
                            } elseif (!$is_own) {
                                $value['member']['nickname'] = substr_cut($value['member']['nickname']);
                            }
                            $after_lottery['address'] += (p_getcolumn('address', array('member_id' => $value['member_id']), 'COUNT(*)') ? : 0);
                        }
                    }
                    $after_lottery['group'] = $luck_group;

                } else {

                    $after_lottery['luck_goods'] = p_getall('luck_goods', array('luck_id' => $id));
                    foreach ($after_lottery['luck_goods'] as $kl => $line) {
                        $line['image'] = getImage($line['image']);
                        $line['info'] = p_getall('luck_goods_info', array('luck_id' => $id, 'luck_goods_id' => $line['id']));
                        foreach ($line['info'] as $kv => $v) {
                            $v['member'] = p_get('member', array('id' => $v['in_member_id']));

                            if ($v['in_member_id'] == $this->member['id']) {
                                $after_lottery['in_my'] = true;
                                $after_lottery['in_my_goods'] = $v;
                                $address = p_get('address', array('member_id' => $this->member['id']));
                                $after_lottery['in_my_address'] = !empty($address);
                            } else if (!$is_own) {
                                $v['member']['nickname'] = substr_cut($v['member']['nickname']);
                            }
                            $after_lottery['address'] += (p_getcolumn('address', array('member_id' => $v['in_member_id']), 'COUNT(*)') ? : 0);
                            $line['info'][$kv] = $v;
                        }
                        $after_lottery['luck_goods'][$kl] = $line;
                    }
                }

            }
            $t = time();
            $sql = "SELECT A.image,A.relation_id,B.* FROM " . prefix('home') . " AS A INNER JOIN " . prefix('luck') . " AS B ON A.relation_id=B.id WHERE A.position=1 AND A.type=1 AND {$t} > A.start AND {$t} < A.end AND B.status = 1 AND A.uniacid=" . UNIACID . " order by rand() LIMIT 2";
            $after_lottery['more'] = p_fetchall($sql);
            if ($after_lottery['more']) {
                foreach ($after_lottery['more'] as & $m) {
                    $m['goods'] = p_get('luck_goods', array('luck_id' => $m['relation_id']));
                    //$m['luck'] = p_get('luck', array('id' => $m['relation_id']));

                    if ($m['lottery_type'] == 1) {
                        $m['lottery'] = date('m月d日 H:i', $m['lottery_value']);
                    } elseif ($m['lottery_type'] == 2) {
                        $m['lottery'] = '满' . $m['lottery_value'] . '人';
                    } else {
                        $m['lottery'] = '发起者手动开奖';
                    }

                    $m['image'] = $m['image'] ? getImage($m['image']) : (getImage($m['goods']['image'] ? $m['goods']['image'] : getConfig('prize_default_picture')) ? : '');
                }
            }
            $luck['receive_url'] && $luck['receive_url'] = urlencode($luck['receive_url']);


        }
        $complaint_msg = getConfig('complaint_msg');
        $complaint_msg = $complaint_msg ? json_decode($complaint_msg, true) : array('我们仅提供发起及参与抽奖的平台,不对第三方发起人行为承担责任。', '如遇虚假或违规抽奖请点击下方按钮进行投诉,审核后我们将对发起人进行处罚');
        $complaint_content = getConfig('complaint_content');
        $complaint_content = $complaint_content ? json_decode($complaint_content, true) : array('虚假抽奖', '恶意营销', '其他违规');

        $screen_ad = getConfig('screen_id');

        $dy_send_message_kj_id = getConfig('dy_send_message_kj_id');
        $new_activities = getConfig('new_activities');

        $dy = array();
        if ($dy_send_message_kj_id) {
            $dy[] = $dy_send_message_kj_id;
        }
        if ($new_activities) {
            $dy[] = $new_activities;
        }

        $integralUser = array(
            'is' => $this->vote->isIntegralUser()
        );
        if ($integralUser['is']) {
            $integralUser['integral_name'] = getConfig('integral_name', '积分');
        }

        if ($disclaimer = getConfig('disclaimer')) {
            $disclaimer = json_decode($disclaimer, true);
        }



        $data = array(
            'luck' => $luck,
            'is_own' => $is_own,
            'launch' => $launch,
            'images' => $images,
            'ad' => getConfig('luck_details_ad'),
            'participate' => $participate,
            'member' => $this->member,
            'is_participate' => $is_participate,
            'is_auth' => $this->is_auth,
            'group' => $group,
            'group_id' => $group_id,
            'after_lottery' => $after_lottery,
            'config' => array(
                'complaint_msg' => $complaint_msg,
                'complaint_content' => $complaint_content,
                'dy_send_message_kj_id' => $dy_send_message_kj_id,
                'disclaimer' => $disclaimer,
                'info_code_tips' => getConfig('info_code_tips', '邀请好友得抽奖码'),
            ),
            'screen_ad' => $screen_ad,
            'adUnitId' => getConfig('video_id'),
            'new_share' => $new_share,
            'yy' => yanyi(),
            'assistance' => $assistance,
            'dy' => $dy,
            'integralUser' => $integralUser
        );

        $this->json($data);
    }


    /**
     *
     */
    public function bindGroup()
    {
        $id = $this->get('id');
        $luck = p_get('luck', array('id' => $id, 'member_id' => $this->member['id'], 'is_delete' => 1, 'complete' => 2, 'appoint_group' => 2));

        if (empty($luck)) {
            $this->json();
        }
        $encryptedData = $this->get('encryptedData');
        $iv = $this->get('iv');
        $wxbd = new WXBizDataCrypt($this->w['uniaccount']['key'], $this->session_key);
        $res = $wxbd->decryptData($encryptedData, $iv, $data);
        if ($res != ErrorCode::$OK) {
            json('unlogin', -1);
        }
        $data = json_decode($data, true);

        p_update('luck', array('group_id' => $data['openGId']), array('id' => $id));
        $this->json();
    }



    /**
     *
     */
    public function prizeAddress()
    {
        $id = $this->get('id');
        $luck = p_get('luck', array('id' => $id, 'member_id' => $this->member['id']));
        if (empty($luck)) {
            $this->json('无查看权限');
        }
        $info = p_getall('luck_goods_info', array('luck_id' => $id));
        foreach ($info as & $value) {
            $address = p_get('address', array('member_id' => $value['in_member_id']));
            $value['address'] = $address ? "{$address['name']}, {$address['phone']}, {$address['address']}" : '';
            $value['member'] = p_get('member', array('id' => $value['in_member_id']));
        }
        $this->json(array('address' => $info));
    }


    /**
     *
     */
    public function lottery()
    {
        $id = $this->get('id');

        $luck = p_get('luck', array('id' => $id, 'member_id' => $this->member['id'], 'is_delete' => 1, 'status' => 1, 'complete' => 2));

        if (empty($luck)) {
            $this->json();
        }

        if ($this->vote->lottery($id)) {
            $this->json();
        }
        $this->json($this->vote->error, 0);
    }


    /**
     *
     */
    public function pLoad()
    {
        $id = $this->get('id');

        $num = p_getcolumn('luck_participate', array('luck_id' => $id), 'COUNT(*)') ? : 0;

        $luck = p_get('luck', array('id' => $id));

        if ($luck['lottery_type'] != 2 && $luck['automatic'] == 2) {
            $num += $this->vote->automatic_number($luck['created']);
        }


        $this->json(array('ad' => getConfig('luck_join'), 'num' => $num));
    }


    /**
     *
     */
    public function participant()
    {
        $p = $this->get('p', 1);
        $id = $this->get('id');
        $page_size = 100;
        $limit = ($p - 1) * $page_size;
        $user = array();

        $num = p_getcolumn('luck_participate', array('luck_id' => $id), 'COUNT(*)') ? : 0;

        if ($limit < $num) {
            $sql = "SELECT B.headimgurl FROM " . prefix('luck_participate') . " AS A LEFT JOIN " . prefix('member') . " AS B ON A.member_id=B.id WHERE A.luck_id ={$id} ORDER BY A.id DESC LIMIT {$limit}, {$page_size}";
            $user = p_fetchall($sql);
        }


        $luck = p_get('luck', array('id' => $id));

        if ($luck['lottery_type'] != 2 && $luck['automatic'] == 2) {

            $z_p = ceil($num / $page_size);


            $count = count($user);


            $fictitious_num = $this->vote->automatic_number($luck['created']);

            if ($count > 0) {
                $j = $fictitious_num - $limit;
                $p = 1;
            } else {
                $j = $fictitious_num - $limit + $num;
                $p = $p - $z_p;
            }

            $page_size = $page_size - $count;


            $limit = ($p - 1) * $page_size;


            if ($j > 0 && $fictitious_num > 0) {

                $page_size = $j > $page_size ? $page_size : $j;

                $sql = "SELECT headimgurl FROM " . prefix('member') . " WHERE fictitious=2 LIMIT {$limit}, {$page_size}";

                if ($automatic = p_fetchall($sql)) {
                    $user = array_merge($user, $automatic);
                }
            }

        }


        $this->json(array('user' => $user));
    }


    /**
     *
     */
    public function complaint()
    {
        $luck_id = $this->get('luck_id');
        $content = $this->get('content');

        if ($luck_id && $content) {
            $insert = array(
                'luck_id' => $luck_id,
                'member_id' => $this->member['id'],
                'content' => $content,
                'created' => time()
            );

            p_insert('luck_complaint', $insert);
        }
        $this->json();
    }


    /**
     *
     */
    public function poster()
    {
        $luck_id = $this->get('id');
        $luck = p_get('luck', array('id' => $luck_id, 'is_delete' => 1, 'complete' => 2));
        if (empty($luck)) {
            $this->json('该抽奖不存在', 0);
        }

        if ($luck['lottery_type'] == 1) {
            $luck['lottery_font'] = date('m月d日 H:i', $luck['lottery_value']) . ' 自动开奖';
        } elseif ($luck['lottery_type'] == 2) {
            $luck['lottery_font'] = '满' . $luck['lottery_value'] . '人 自动开奖';
        } else {
            $luck['lottery_font'] = '发起者手动开奖';
        }

        $bg = getConfig('poster_bg_image');

        $bg = $bg ? json_decode($bg, true) : array();

        if ($bg) {
            foreach ($bg as & $value) {
                $value['icon'] = getImage($value['icon']);
                $value['bg'] = getImageLocal($this, $value['bg']);
                $value['color'] = $value['color'] ? : '#ffffff';
            }
        }
        $xcx_path = 'bh_bargain/pages/userGame/userGame';

        $group_id = 0;

        $participate = p_get('luck_participate', array('luck_id' => $luck_id, 'member_id' => $this->member['id']));
        if ($participate && $participate['group_id'] > 0) {
            $group_id = $participate['group_id'];
        }

        $code = $this->vote->createWxCode($this, $this->member['id'] . '_' . $luck_id . '_' . $group_id, $xcx_path, 'luck' . $luck_id . '-' . $group_id . '_' . $this->member['id'] . '.png');

        $member = $this->member;

        $luck_info = p_getall('luck_goods', array('luck_id' => $luck_id));
        foreach ($luck_info as & $value) {
            if ($value['image']) {
                $value['image'] = getImageLocal($this, $value['image']);
            } else {
                $image = getConfig('prize_default_picture');
                if ($image) {
                    $value['image'] = getImageLocal($this, $image);
                } else {
                    $value['image'] = $this->image_url . 'prize_default_picture.png';
                }
            }

        }

        $images = array(
            'bg_img' => $this->image_url . 'images/bg2.jpg',
            'white' => $this->image_url . 'white.png',
            'checked' => $this->image_url . 'checked.png',
        );
        $luck['lottery_explain'] = str_replace(PHP_EOL, '', $luck['lottery_explain']);

        $data = array(
            'member' => $member,
            'luck_info' => $luck_info,
            'images' => $images,
            'bg' => $bg,
            'code' => $code,
            'head' => headLocal($this, $this->member['headimgurl']),
            'luck' => $luck
        );

        $this->json($data);
    }

    /**
     *
     */
    public function share()
    {
        $luck_id = $this->get('id');
        $res = $this->vote->getShaerImg($luck_id, $this);
        Header("Content-Type: image/png");
        echo file_get_contents($res['path']);
        exit;
    }


    /**
     *
     */
    public function embed()
    {
        $luck_id = $this->get('id');
        //$res = $this->vote->getShaerImg($luck_id, $this);


        $luck_goods = p_get('luck_goods', array('luck_id' => $luck_id), array(), '', 'id desc');

        if ($luck_goods['image']) {
            $image = getImageLocal($this, $luck_goods['image']);
        } else {
            $image = getConfig('prize_default_picture');
            if ($image) {
                $image = getImageLocal($this, $image);
            } else {
                $image = $this->image_url . 'prize_default_picture.png';
            }
        }
        $luck = p_get('luck', array('id' => $luck_id));

        if ($luck['lottery_type'] == 1) {
            $font_text = date('m月d日 H:i', $luck['lottery_value']) . ' 自动开奖';
        } elseif ($luck['lottery_type'] == 2) {
            $font_text = '满' . $luck['lottery_value'] . '人 自动开奖';
        } else {
            $font_text = '发起者手动开奖';
        }
        $font = '奖品: ' . $luck_goods['name'];

        $data = array(
            //'url' => $res['url'],
            'appid' => $this->w['uniaccount']['key'],
            'image' => $image,
            'font_text' => $font_text,
            'font' => $font
        );


        $this->json($data);
    }


    /**
     *
     */
    public function apply()
    {
        $p = $this->get('p', 1);
        $page_size = 8;
        $limit = ($p - 1) * $page_size;
        $where = array('member_id' => $this->member['id']);
        if ($this->get('type') == 2) {
            $where['in_prize'] = 2;
        }

        $apply = p_getall('luck_participate', $where, array(),'', array('id desc'), "{$limit}, {$page_size}");
        if ($apply) {
            foreach ($apply as & $value) {
                $value['luck'] = p_get('luck', array('id' => $value['luck_id']));
                $value['luck']['goods'] = p_getall('luck_goods', array('luck_id' => $value['luck_id']));
                $value['luck']['goods'][0]['image'] = getImage($value['luck']['goods'][0]['image']) ? : $this->images('prize_default_picture', 'prize_default_picture.png');

                $value['member'] = p_get('member', array('id' => $value['luck']['member_id']));


                if ($value['luck']['lottery_type'] == 1) {
                    $value['lottery'] = date('m月d日 H:i', $value['luck']['lottery_value']);
                } elseif ($value['luck']['lottery_type'] == 2) {
                    $value['lottery'] = '满' . $value['luck']['lottery_value'] . '人';
                } else {
                    $value['lottery'] = '';
                }
            }
        }

        $data = array('apply' => $apply);
        $this->json($data);
    }


    /**
     *
     */
    public function myLaunch()
    {
        $p = $this->get('p', 1);
        $page_size = 8;
        $limit = ($p - 1) * $page_size;
        $launch = p_getall('luck', array('member_id' => $this->member['id'], 'complete' => 2), array(),'', array('id desc'), "{$limit}, {$page_size}");

        if (!empty($launch)) {
            foreach ($launch as & $value) {
                $value['goods'] = p_getall('luck_goods', array('luck_id' => $value['id']));
                $value['goods'][0]['image'] = getImage($value['goods'][0]['image']) ? : $this->images('prize_default_picture', 'prize_default_picture.png');

                if ($value['lottery_type'] == 1) {
                    $value['lottery'] = date('m月d日 H:i', $value['lottery_value']);
                } elseif ($value['lottery_type'] == 2) {
                    $value['lottery'] = '满' . $value['lottery_value'] . '人';
                } else {
                    $value['lottery'] = '';
                }


                if ($value['examine'] == 2) {
                    $value['locking'] = 2;
                    $value['msg'] = $value['status'] > 1 ? '已结束' : '进行中';
                } else {
                    $value['msg'] = $value['examine'] == 1 ? '审核中' : '审核未通过';
                    $value['locking'] = $value['examine'];
                }
            }
        }
        $data = array('launch' => $launch);
        $this->json($data);
    }


    /**
     *
     */
    public function sponsor()
    {
        $sponsor = p_getall('luck_sponsor', array('member_id' => $this->member['id']));
        $sponsor = arrayImage($sponsor, 'code');

        $data = array(
            'sponsor' => $sponsor
        );
        $this->json($data);
    }


    /**
     *
     */
    public function delSponsor()
    {
        $id = $this->get('id');
        p_delete('luck_sponsor', array('id' => $id, 'member_id' => $this->member['id']));
        $this->json();
    }


    /**
     *
     */
    public function addSponsor()
    {
        $type = $this->get('type', 2);
        $name = $this->get('name');

        $data = array(
            'member_id' => $this->member['id'],
            'type' => $type,
            'name' => $name,
            'created' => time()
        );
        if (in_array($type, array(2, 5))) {
            $data['code'] = $this->get('code');
        } else if($type != 3){
            $data['account'] = $this->get('account');
        }

        p_insert('luck_sponsor', $data);
        $this->json();
    }



}